//
//  HSPImageAppreciateViewController.m
//  Icar
//
//  Created by dllo on 15/10/16.
//  Copyright © 2015年 小龙侠. All rights reserved.
//

#import "HSPImageAppreciateViewController.h"
#import "BaseHeader.h"
#import "HSPSecondCell.h"
#import "AFNBlock.h"
@interface HSPImageAppreciateViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong)UITableView *tableViewImage;
@property (nonatomic,assign)BOOL isUp;
@property (nonatomic,assign)NSInteger page;
@property (nonatomic,strong)NSMutableArray *imageArrayName;
@end

@implementation HSPImageAppreciateViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.view setBackgroundColor: [UIColor whiteColor]];
    [self creatable];
    [self AlbumaHeader];
    [self AlbumaFooter];
    self.imageArrayName = [[NSMutableArray alloc]init];
}
#pragma 上拉刷新
- (void)AlbumaHeader
{
    __block HSPImageAppreciateViewController *vc = self;
    [_tableViewImage addLegendHeaderWithRefreshingBlock:^{
        vc.page = 1;
        [vc APPadndatanumber :1];
        _isUp = YES;
    }];
    [_tableViewImage.header beginRefreshing];
}
#pragma 下拉加载
- (void)AlbumaFooter
{
    __block HSPImageAppreciateViewController *vcc = self;
    [_tableViewImage addLegendFooterWithRefreshingBlock:^{
        vcc.page = vcc.page + 1;
        [vcc APPadndatanumber:vcc.page];
        _isUp = NO;
    }];
}
- (void)APPadndatanumber:(NSInteger)number
{
    if ([Reachability isConnectedInternet] == YES) {
        [AFNBlock getDataWithString:[NSString stringWithFormat:@"http://agent1.pconline.com.cn:8941/pcautophoto/iphone_cate_json.jsp?id=89"] block:^(id back) {
            if (_isUp == YES) {
                [self.imageArrayName removeAllObjects];
            }
            for (NSDictionary *dic in [back objectForKey:@"groups"]) {
                HSPImageAppreciate *model = [HSPImageAppreciate modelWithDictionary:dic];
                [self.imageArrayName addObject:model];
               
            }
            [self.tableViewImage reloadData];
            [self.tableViewImage.header endRefreshing];
            [self.tableViewImage.footer endRefreshing];
        }];
    }
}
- (void)creatable
{
    _tableViewImage = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 147) style:UITableViewStylePlain];
    _tableViewImage.delegate =self;
    _tableViewImage.dataSource = self;
    [self.view addSubview:_tableViewImage];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _imageArrayName.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 180;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HSPSecondCell *cell = [tableView dequeueReusableCellWithIdentifier:@"image"];
    if (cell == nil) {
        cell = [[HSPSecondCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"image"];
    }
    HSPImageAppreciate *data = [self.imageArrayName objectAtIndex:indexPath.row];
    tableView.separatorStyle = NO;
    self.tableViewImage.scrollsToTop = YES;
    [cell getImage:data];
    return cell;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
